package com.easyjop.Controller;

import com.easyjop.annotation.GlobalInterceptor;
import com.easyjop.annotation.VerifyParam;
import com.easyjop.entity.enums.PermissionCodeEnum;
import com.easyjop.entity.po.Category;
import com.easyjop.entity.query.CategoryQuery;
import com.easyjop.entity.vo.ResponseVO;
import com.easyjop.service.CategoryService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/category")
public class CategoryController extends ABaseController{
    @Resource
    private CategoryService categoryService;
    @RequestMapping("/loadAllCategory")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.CATEOGRY_LIST)
    public ResponseVO loadDataList(CategoryQuery query,Integer bankId) {
        query.setOrderBy("sort asc");
        query.setType(bankId);
        return getSuccessResponseVO(categoryService.findListByPage(query));
    }
    @RequestMapping("/saveCategory")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.CATEOGRY_EDIT)
    public ResponseVO saveCategory(Category category,Integer bankId) {
        categoryService.saveCategory(category,bankId);
        return getSuccessResponseVO(null);
    }
    @RequestMapping("/delCategory")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.CATEOGRY_EDIT)
    public ResponseVO delCategory(@VerifyParam(required = true) Integer categoryId) {
        categoryService.deleteCategoryByCategoryId(categoryId);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/changeSort")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.CATEOGRY_EDIT)
    public ResponseVO changeSort(@VerifyParam(required = true) String categoryIds) {
        categoryService.changeSort(categoryIds);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/loadAllCategory4Select")
    @GlobalInterceptor
    public ResponseVO loadAllCategory(@RequestParam(required = false) Integer type,Integer bankId) {
        if (bankId==null){
            bankId=type;
        }
        return getSuccessResponseVO(categoryService.loadAllCategoryByQuery(bankId));
    }
}
